import { Component } from '@angular/core';
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ConfigProvider } from "../../providers/config/config";
import { ValidateProvider } from "../../providers/validate/validate";
import { UtilsProvider } from "../../providers/utils/utils";

/**
 * Generated class for the CollectListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage({
  segment: 'CollectListPage',
  defaultHistory: ['CollectInforPage', 'HomePage']
})
@Component({
  selector: 'page-collect-list',
  templateUrl: 'collect-list.html',
})
export class CollectListPage {
  page: number = 1;
  infolist = [];
  constructor(public navCtrl: NavController,
    public httpService: HttpServicesProvider,
    public config: ConfigProvider,
    public validate: ValidateProvider,
    public navParams: NavParams,
    public util: UtilsProvider) {
  }
  ionViewCanEnter() {
    return this.validate.checkLogin();
  }

  ionViewDidLoad() {
    this.getData(null);
  }

  getData(infiniteScroll) {
    // list data
    this.httpService.requestPostData('/bottle_collect/collect_list', {
      page: this.page,
      token: this.config.getToken()
    }, (response) => {
      let data = response['data'];
      this.infolist = this.infolist.concat(data['lists']);
      this.page++;
      if (infiniteScroll) {
        infiniteScroll.complete();
        /*请求完成数据更新页面*/
        if (data.total > this.infolist.length) {   /*没有数据 禁用上拉更新*/
          this.infolist = data['lists'];
        } else {
          infiniteScroll.enable(false);
        }
      } else {
        if (infiniteScroll) {
          infiniteScroll.enable(false);
        }
      }
    });
  }
  doInfinite(infiniteScroll) {
    this.getData(infiniteScroll);
  }
  goPage(page, data = {}) {
    this.navCtrl.push(page, data).then(success => {
      if (success !== true) {//页面需要登录后跳转
        this.navCtrl.push('LoginPage', data);
      }
    })
  }
}
